package org.zizi.db.plan.reader;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zizi.db.column.ColumnReader;
import org.zizi.db.plan.ResultReader;

public class MapResultReader<T> implements ResultReader<T>
{
	private Class<T> type;
	private List<String> columns;
	private List<ColumnReader> readers;
	
	public MapResultReader(Class<T> type, List<String> columns, List<ColumnReader> readers)
	{
		this.type = type;
		this.columns = columns;
		this.readers = readers;
	}
	
	public T build(ResultSet rs) throws Exception
	{
		Map<String, Object> map = new HashMap<String, Object>();
		
		int n = columns.size();
		for (int i = 0; i < n; i++)
		{
			String name = columns.get(i);
			ColumnReader reader = readers.get(i);
			
			map.put(name, reader.read(rs, i + 1));
		}
		
		return type.cast(map);
	}
}
